package ch04;

public class Ch4_3 {
    public static void createMinBst(int elements[], int begin, int end, BinaryNode<Integer> node) {
	if (begin <= end) {
	    int mid = (int)((begin + end) / 2.0);
	    node.element = elements[mid];
	    if (begin <= mid - 1) {
	       	node.left = new BinaryNode<Integer>();
	       	node.left.parent = node;
	       	createMinBst(elements, begin, mid - 1, node.left);
	    }
	    if (mid + 1 <= end) {
	    	node.right = new BinaryNode<Integer>();
	    	node.right.parent = node;
	    	createMinBst(elements, mid + 1, end, node.right);
	    }
	}
    }

    public static void main(String[] args) {
	int limit = 100;
	int elements[] = new int[limit];
	for (int i = 0; i < limit; i++) {
	    elements[i] = i;
	}

	BST<Integer> tree = new BST<Integer>();
	tree.setRoot(new BinaryNode<Integer>());
	createMinBst(elements, 0, elements.length - 1, tree.getRoot());
	BinaryNode<Integer> x = tree.min(tree.getRoot());
	while (null != x) {
	    System.out.println(x);
	    x = tree.successor(x);
	}
    }
}